import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:flutter_study/review/reudx2/book.dart';
import 'package:flutter_study/review/reudx2/book_reducer.dart';
import 'package:flutter_study/review/reudx2/redux_state.dart';
import 'package:flutter_study/review/reudx2/user.dart';
import 'package:flutter_study/review/reudx2/user_reducer.dart';
import 'package:redux/redux.dart';

void main() {
  Store _store = Store<ReduxState>(getRedux,
      middleware: middleware,
      initialState: ReduxState(user: User.initUser(), book: Book.initBook()));
  runApp(MyApp(_store));
}

class MyApp extends StatelessWidget {
  Store _store;

  MyApp(this._store);

  @override
  Widget build(BuildContext context) {
    return StoreProvider<ReduxState>(
      store: _store,
      child: MaterialApp(
        title: 'Material App',
        home: FirstPage(),
      ),
    );
  }
}

/// 第一个页面
class FirstPage extends StatefulWidget {
  const FirstPage({Key key}) : super(key: key);

  @override
  _FirstPageState createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  @override
  Widget build(BuildContext context) {
    return StoreBuilder<ReduxState>(builder: (ctx, store) {
      return Scaffold(
        appBar: AppBar(
          title: Text('redux first page'),
        ),
        body: Padding(
          padding: EdgeInsets.only(top: 30, left: 30, right: 30),
          child: Column(
            children: [
              Text("User Name:${store.state.user.name}"),
              SizedBox(
                height: 15,
              ),
              Text("Book Name:${store.state.book.name}"),
              SizedBox(
                height: 15,
              ),
              ElevatedButton(
                  onPressed: () => Navigator.of(context)
                      .push(MaterialPageRoute(builder: (ctx) => SecondPage())),
                  child: Text('jump')),
            ],
          ),
        ),
      );
    });
  }
}

/// 第二个页面
class SecondPage extends StatefulWidget {
  const SecondPage({Key key}) : super(key: key);

  @override
  _SecondPageState createState() => _SecondPageState();
}

class _SecondPageState extends State<SecondPage> {
  @override
  Widget build(BuildContext context) {
    return StoreBuilder<ReduxState>(builder: (ctx, store) {
      return Scaffold(
        appBar: AppBar(
          title: Text('redux second page'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            children: [
              Text("User Name:${store.state.user.name}"),
              SizedBox(
                height: 15,
              ),
              Text("Book Name:${store.state.book.name}"),
              SizedBox(
                height: 15,
              ),
              ElevatedButton(
                  onPressed: () =>
                      store.dispatch(AddUserAction(store.state.user)),
                  child: Text('add user')),
              SizedBox(
                height: 15,
              ),
              ElevatedButton(
                  onPressed: () =>
                      store.dispatch(UpdateUserAction(store.state.user)),
                  child: Text('update user')),
              SizedBox(
                height: 15,
              ),
              ElevatedButton(
                  onPressed: () =>
                      store.dispatch(AddBookAction(store.state.book)),
                  child: Text('add book')),
              SizedBox(
                height: 15,
              ),
              ElevatedButton(
                  onPressed: () =>
                      store.dispatch(UpdateBookAction(store.state.book)),
                  child: Text('update book')),
            ],
          ),
        ),
      );
    });
  }
}
